package com.test.clyq.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.QuestionCategoryDao;
import com.test.clyq.entity.QuestionCategory;
import com.test.clyq.entity.Users;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.Pagination;
import com.test.util.SessionFactoryUtil;

/**
 * QuestionCategoryDaoHibernate. <br>
 * 问题类别管理.
 * <p>
 * Copyright: Copyright (c) 2010-3-17 下午04:30:46
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@SuppressWarnings("unchecked")
public class QuestionCategoryDaoHibernate extends BaseDaoHibernate implements QuestionCategoryDao {

	/**
	 * 添加问题类别
	 * 
	 * @param file
	 * @param user
	 */
	public void addQuestionCategory(QuestionCategory file, Users user) {
		this.log.info("新增问题类别start");
		try {
			saveOrUpdateItem(file, user);
			this.log.debug("新增问题类别成功");
			this.log.info("新增问题类别end");
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]addQuestionCategory发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}

	/**
	 * 删除问题类别
	 * 
	 * @param id
	 * @param user
	 */
	public boolean deleteQuestionCategory(Long id, Users user) {
		this.log.info("根据问题类别ID删除问题类别start");
		try {

			if (user != null) {
				removeItem(QuestionCategory.class, id, user);
				this.log.debug("删除问题类别成功");
				this.log.info("根据问题类别ID删除问题类别end");
				return true;
			} else {
				this.log.debug("删除问题类别失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]deleteQuestionCategory发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	/**
	 * 获取一个问题类别
	 */
	public QuestionCategory getOneQuestionCategory(Long id) {
		this.log.info("根据问题类别ID获得问题类别start");
		try {
			QuestionCategory file = (QuestionCategory) getItem(QuestionCategory.class, id);
			if (file != null) {
				this.log.info("根据问题类别ID获得问题类别end");
				return file;
			} else {
				this.log.info("根据问题类别ID获得问题类别end");
				return new QuestionCategory();
			}

		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]getOneQuestionCategory发生错误！\n" + FormatExceptionStack.formatException(e));
			return new QuestionCategory();
		}
	}

	/**
	 * 通过分页获取问题类别
	 * 
	 * @param pagination
	 * @return
	 */
	public List<QuestionCategory> getQuestionCategoryByPage(Pagination pagination) {
		this.log.info("获得问题类别列表start");
		String sql = "from QuestionCategory";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("select count(*) " + sql);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得问题类别列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<QuestionCategory>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]getQuestionCategoryByPage发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<QuestionCategory>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}

	}

	/**
	 * 获取所有问题类别
	 * 
	 * @return
	 */
	public List<QuestionCategory> getQuestionCategorys() {
		this.log.info("获得现在文件列表start");
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("from QuestionCategory order by corder desc");
			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得问题类别列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<QuestionCategory>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]getQuestionCategorys发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<QuestionCategory>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}

	}

	@Override
	public List<QuestionCategory> getQuestionCategorys(Integer website) {
		this.log.info("获得现在文件列表start");
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("from QuestionCategory order by corder desc");
			// query.setInteger("website", website);
			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得问题类别列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<QuestionCategory>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]getQuestionCategorys发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<QuestionCategory>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 修改问题类别
	 * 
	 * @param id
	 * @param user
	 */
	public void updateQuestionCategory(QuestionCategory file, Users user) {
		this.log.info("保存或更新问题类别start");
		try {
			saveOrUpdateItem(file, user);
			this.log.debug("保存或更新问题类别成功");
			this.log.info("保存或更新问题类别end");
		}
		catch (DataAccessException e) {
			this.log.error("[QuestionCategory]updateQuestionCategory发生错误！\n" + FormatExceptionStack.formatException(e));
		}
	}

}
